package com.guaniu.voip;

import static net.gotev.sipservice.SipTlsUtils.TAG;

import android.app.Application;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.res.Resources;
import android.hardware.camera2.CameraManager;
import android.util.Log;

import com.guaniu.voip.utils.Utils;

import org.pjsip.PjCameraInfo2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MyApplication extends Application {
    public static String TAG = "MyApplication";
    private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
    private static Context mAppContext;
    static KeyguardManager sKeyguardManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = getApplicationContext();

        /* 初始化工具服务 */
        Utils.init(this);

        CameraManager cm = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
        PjCameraInfo2.SetCameraManager(cm);

        CrashHandler.getInstance().init(this);
        //load
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                //CSVUtils.loadCSVData(mContext);
            }
        });
    }

    public static KeyguardManager getKeyguardManager(){
        if (mAppContext == null){
            return sKeyguardManager;
        }
        if(sKeyguardManager == null){
            sKeyguardManager = (KeyguardManager)mAppContext.getSystemService(Context.KEYGUARD_SERVICE);
        }
        return sKeyguardManager;
    }

    /**
     * Called when the overall system is running low on memory
     */
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.w(TAG, "System is running low on memory");

    }

    /**
     * @return the main resources from the Application
     */
    public static Resources getAppResources()
    {
        if(mAppContext == null) return null;
        return mAppContext.getResources();
    }
    public static Context getInstance() {
        return mAppContext;
    }
}
